<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Supervisor_feedback extends WxHomeController {

	function __construct() {
		parent::__construct();
		$this -> sidebar = "Supervisor_feedback";		
		$this -> load ->model("Admin_model");
		$this -> load ->library("ProjectService/feedback_service");
		$this -> load ->model("Supervisor_feedback_config_model");
		
	}
	function Index(){
		$this -> load ->view("home/supervisor_feedback");
	}

	function checkSubrole(){
		$connar['items']='id,usercode,username,role_id,sub_role_id';
		$connar['where']=$this->sqlEscape("id=?? ",array($_SESSION[SESS_USER_HOME]['id']));
		$result=$this-> Admin_model ->get_single_full($connar);
		// var_dump($this->db->last_query());
		// exit();
		if($result)
		{			
			if($result['sub_role_id']!=null ||$result['sub_role_id']!=''){
				$data['check_role']=1;
				$html="<select id='sub_role' class='selectpicker'><option value='0' checked>请选择反馈身份</option>";
				$sub_role=explode(',',$result['sub_role_id']);
				array_push($sub_role,$result['role_id']);
				foreach ($sub_role as $k => $v) {
					$role_name=(new WebRole)->show($v);
					$html=$html.'<option value='.$v.'>'.$role_name.'</option>';
				}
				$html=$html."</select>";
				$data['html']=$html;
				exit(retJson("请选择反馈身份！",true,$data));
			}
		}
	}
	function checkSubrole1(){
		$connar['items']='id,usercode,username,role_id,sub_role_id';
		$connar['where']=$this->sqlEscape("id=?? ",array($_SESSION[SESS_USER_HOME]['id']));
		$result=$this-> Admin_model ->get_single_full($connar);
		// var_dump($result);
		// var_dump($this->db->last_query());
		// exit();		
		$data=array();
		if($result){			
			if($result['sub_role_id']!=null ||$result['sub_role_id']!=''){
				$sub_role=explode(',',$result['sub_role_id']);
				array_push($sub_role,$result['role_id']);
				foreach ($sub_role as $k => $v) {
					$role_name=(new WebRole)->show($v);
					if($v==WebRole::SchoolSupervisor[XPHEnum::Value] || $v==WebRole::UnitSupervisor[XPHEnum::Value] || $v==WebRole::Manage[XPHEnum::Value] || $v==WebRole::UnitSupervisorGroupLeader[XPHEnum::Value]){					
						array_push($data,array("role_id"=>$v,"role_name"=>$role_name));
					}				
				}				
			}
			exit(retJson("请选择反馈身份！",true,$data));
		}
	}

	function addFeedback(){
		if(!$_POST['content'] || $_POST['content']==''){
			exit(retJson("反馈内容不能为空",false));
		}else{
			$param['content']=$_POST['content'];
		}		
		$param['creator']=$_SESSION[SESS_USER_HOME]['id'];
		if(!$_POST['category'] || $_POST['category']=='0'){
			exit(retJson("反馈类型不能为空",false));
		}else{
			$param['category']=$_POST['category'];
		}
		
		$result = $this -> Admin_model->get_single(array("id"=>$param['creator']));	
		//$table=(new FeedbackType)->showByIndex($feedback_type,XPHEnum::Remark);
		if(isset($_POST['login_role'])){
			// $param['type']=$_POST['login_role'];
			if($_POST['login_role']=='0'){
				exit(retJson("请刷新并选择反馈身份",false));
			}
			if($_POST['login_role']==WebRole::SchoolSupervisor[XPHEnum::Value]){
				$param['type']=SupervisorFeedbackType::School[XPHEnum::Value];
			}else if($_POST['login_role']==WebRole::UnitSupervisor[XPHEnum::Value]){
				$param['type']=SupervisorFeedbackType::Secondary[XPHEnum::Value];				
			}else{
				// exit(retJson("不是校督导或二级单位督导不能提交反馈！",false));
				$role=explode(',',$_SESSION[SESS_USER_HOME]['sub_role_id']);
				array_push($role,$_SESSION[SESS_USER_HOME]['role_id']);
				if(in_array(WebRole::SchoolSupervisor[XPHEnum::Value],$role)){
					$param['type']=SupervisorFeedbackType::School[XPHEnum::Value];
				}else if(in_array(WebRole::UnitSupervisor[XPHEnum::Value],$role)){
					$param['type']=SupervisorFeedbackType::Secondary[XPHEnum::Value];
				}else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value],$role)){
					$param['type']=SupervisorFeedbackType::GroupLeader[XPHEnum::Value];
				}else if(in_array(WebRole::Manage[XPHEnum::Value],$role)){
					$param['type']=SupervisorFeedbackType::Admin[XPHEnum::Value];
				}else if(in_array(WebRole::OutsideSupervisor[XPHEnum::Value],$role)){
					$param['type']=SupervisorFeedbackType::Outside[XPHEnum::Value];
				}
			}
		}else{
			$param['type']=$result['admin_type'];
		}
		$param['dept_name']=$result['dept_name'];
		$param['class_name']=$result['class_name'];
		if(isset($_POST['img'])){
			$img=$this->NOTXSS_POST['img'];
			$param['img']=array();
			for ($i=0;$i<=count($img)-1;$i++){
				$item = $img[$i];
				if (checkStringIsBase64($item)) {
					$checkRet = uploadImg($item, 'feedback/supervisor');
					if (!$checkRet['status']) {
						exit(toRetJson($checkRet));
					}
					$data=$checkRet["v"];
					
				} else {
					exit(retJson('上传的图片格式不正确！', false));
				}
				array_push($param['img'], $data); 
			}
		}
		$data=$this -> feedback_service ->saveFeedBack(FeedbackType::Supervisor[XPHEnum::Value],$param);
		exit($data);	
		
	}
	function getType(){
		$result=$this -> Supervisor_feedback_config_model->get_list(array("status"=>CommonStatus::UnDeleted[XPHEnum::Value]));
		exit(retJson("获取反馈类型",true,$result));
	}

}